﻿using System;
using DatabaseGroup.SSIS.SourceAdapters.Parser;

namespace DatabaseGroup.UnitTests.SSIS.SourceAdapters.Parser
{
    /// <summary>
    /// Implements dummy IFileReader functionality to aid testing
    /// </summary>
    internal class FileReaderTestImpl : IFileReader
    {
        private bool _randomCharacters = true;
        private string _sampleString = string.Empty;
        private int _count = 0;
        private int _currentIndex = 0;
        private Random _randomGenerator = new Random();
        private bool _isEof = false;

        /// <summary>
        /// Initializes a new instance of FileReaderTestImpl using the sample string
        /// </summary>
        /// <param name="sample">The sample string to use as source data</param>
        internal FileReaderTestImpl(string sample)
        {
            _sampleString = sample;
            _randomCharacters = false;
        }

        /// <summary>
        /// Initializes a new instance of FileReaderTestImpl using the specified character count
        /// </summary>
        /// <param name="count">The number of random characters to use as source data</param>
        internal FileReaderTestImpl(int count)
        {
            _count = count;
        }

        /// <summary>
        /// Initializes a new instance of FileReaderTestImpl using 
        /// the sample string with a specific character count
        /// </summary>
        /// <param name="sample">The sample string to use as source data</param>
        /// <param name="count">The max number of characters to read from the source data</param>
        internal FileReaderTestImpl(string sample, int count)
        {
            _sampleString = sample;
            _count = count;
            _randomCharacters = false;
        }

        #region IFileReader Members

        public char GetNextChar()
        {
            char retChar = '\0';

            if (_randomCharacters)
            {
                if (_currentIndex < _count)
                {
                    retChar = (char)_randomGenerator.Next(char.MaxValue, char.MaxValue);
                    _currentIndex++;
                }
                else
                {
                    _isEof = true;
                }
            }
            else
            {
                if (_count > 0)
                {
                    if (_currentIndex < _count)
                    {
                        retChar = _sampleString[_currentIndex % _sampleString.Length];
                        _currentIndex++;
                    }
                    else
                    {
                        _isEof = true;
                    }
                }
                else if (_currentIndex < _sampleString.Length)
                {
                    retChar = _sampleString[_currentIndex];
                    _currentIndex++;
                }
                else
                {
                    _isEof = true;
                }
            }

            return retChar;
        }

        public bool IsEOF
        {
            get 
            {
                return _isEof;
            }
        }

        #endregion
    }
}
